package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.base.entity.SystemCopInterface;
import com.mrd.gtimp.base.entity.SystemInterface;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.base.service.SystemCopInterfaceBiz;
import com.mrd.gtimp.base.service.SystemInterfaceBiz;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.annotation.Resource;


/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */

@Controller
@RequestMapping("/base")
public class SystemCopInerfaceAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SystemCopInterfaceBiz systemCopInterfaceBiz;
    @Resource
    private SystemInterfaceBiz systemInterfaceBiz;

    /**
     * 主页面入口
     */
    @RequestMapping("/itf-main")
    public String main() {
        return "/gtimp/base/interface/main";
    }

    /**
     * 模板页面入口
     */
    @RequestMapping("/itf-template")
    public String template() {
        return "/gtimp/base/interface/itf-list";
    }

    /**
     * 展示所有接口模板
     */
    @RequestMapping("/itf-templateData")
    @ResponseBody
    public Page<SystemInterface> templateData() {
        try {
//            sysInterfaces = systemInterfaceBiz.findByJqgrid(0, 0, filters, new Sort.Order("name"));
            Pageable page = new PageRequest(0, 15, new Sort("name"));
            Jqgrid jqgrid = new Jqgrid(new Filter(), page);
            Page<SystemInterface> sysInterfaces = systemInterfaceBiz.findByJqgrid(jqgrid);
            if (sysInterfaces.getSize() > 0) {
                return sysInterfaces;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 展示公司接口
     */
    @RequestMapping("/itf-list")
    @ResponseBody
    public Page<SystemCopInterface> list() {
        try {
//            page = initPage(systemCopInterfaceBiz.findEntityCount(filters));
//            order.add(sidx, sord);
            Filter filters = new Filter();
            Pageable page = new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "id"));
            Jqgrid jqgrid = new Jqgrid(filters, page);
            Page<SystemCopInterface> pageResult = systemCopInterfaceBiz.findByJqgrid(jqgrid);
            if (pageResult != null) {
                return pageResult;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 验证某个公司的接口是否齐全
     */
    @RequestMapping("/itf-vitf")
    public List<SystemInterface> vitf(Company company) {
        try {
            List<SystemInterface> sysInterfaces = null;
            String cid = company.getId();
            String type = HttpUtils.getParam("type");
            sysInterfaces = systemInterfaceBiz.findSystemInterface(cid, type);
            return sysInterfaces;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成接口
     */
    @RequestMapping("/itf-create")
    @ResponseBody
    public Result create(User user, Company company) {
        Result msg;
        msg = Result.newFailInstance(MsgConstants.OPERATION_FAIL);
        try {
            String cid = company.getId();
            String type = HttpUtils.getParam("type");
            boolean result = systemCopInterfaceBiz.CreateInterfaceByTemplate(cid, type, user
                    .getAccount());
            if (result) {
                msg = Result.newSuccessInstance(MsgConstants.OPERATION_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }


}
